package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.KnowledgeType;
import com.mxpio.erp.common.equipment.service.KnowledgeTypeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "KnowledgeTypeController", description = "知识类型")
@RestController
@RequestMapping("/erp/equipment/knowledge/type")
public class KnowledgeTypeController {

    @Autowired
    private KnowledgeTypeService service;

    @GetMapping("list")
    @Operation(summary = "知识类型列表", description = "获取知识类型列表（无分页）", method = "GET")
    public Result<List<KnowledgeType>> list(@Parameter(description="查询参数") Criteria criteria) {
        List<KnowledgeType> list = service.list(KnowledgeType.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("tree")
    @Operation(summary = "知识类型树", description = "获取知识类型树", method = "GET")
    public Result<List<KnowledgeType>> tree(Criteria criteria){
        List<KnowledgeType> knowledgeTypes = service.tree(KnowledgeType.class, criteria);
        return Result.OK(knowledgeTypes);
    }

    @GetMapping("page")
    @Operation(summary = "知识类型列表", description = "获取知识类型列表（分页）", method = "GET")
    public Result<Page<KnowledgeType>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<KnowledgeType> pageResult = service.listPage(KnowledgeType.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取知识类型列表", description = "根据ID获取知识类型列表", method = "GET")
    public Result<KnowledgeType> getById(@PathVariable(name = "id", required = true) String id) {
        KnowledgeType knowledgeType = service.getById(KnowledgeType.class, id);
        return Result.OK(knowledgeType);
    }

    @RequestMapping("add")
    @Operation(summary = "保存知识类型", description = "保存知识类型")
    public Result<KnowledgeType> add(@RequestBody KnowledgeType knowledgeType) {
        service.save(knowledgeType,new SmartCrudPolicyAdapter(){
        });
        return Result.OK(knowledgeType);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑知识类型", description = "编辑知识类型", method = "PUT")
    public Result<KnowledgeType> edit(@RequestBody KnowledgeType knowledgeType) {
        service.update(knowledgeType);
        return Result.OK(knowledgeType);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除知识类型", description = "删除知识类型,多个id用英文逗号分隔", method = "DELETE")
    public Result<?> remove(@PathVariable(name = "ids", required = true) String ids) {
        return service.removeByIds(ids);
    }
}
